package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.TextView;

import com.yocard.yxyd.utils.BigDecimalUtil;
import com.yocard.yxyd.utils.util.TimeUtils;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.http.model.response.CoinsBean;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/4/10.
 * 交易记录列表适配器 19/05/04
 */
public class TransactionAdapter extends RecyclerView.Adapter<TransactionAdapter.ViewHolder> {
    private static final ThreadLocal<SimpleDateFormat> SDF_THREAD_LOCAL = new ThreadLocal<>();
    private List<CoinsBean> list;
    private ValueCallback<CoinsBean> callback;

    public TransactionAdapter(List<CoinsBean> list){
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_transtion, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        CoinsBean bean = list.get(i);
        viewHolder.tvTime.setText(TimeUtils.millis2String(bean.getCreated_at()*1000,getTimeFormat()));
        String coinPrefix;
        if (bean.getStreamable_type_string().equals("Order")){
            coinPrefix = "-";
            viewHolder.tvCount.setTextColor(AppContext.getColor_(R.color.color_ff001f));
        } else {
            coinPrefix = "+";
            viewHolder.tvCount.setTextColor(AppContext.getColor_(R.color.color_2bd321));
        }
        viewHolder.tvAmount.setText(BigDecimalUtil.getFormatCoin(bean.getRemain_coin()));
        viewHolder.tvCount.setText(coinPrefix + BigDecimalUtil.getFormatCoin(bean.getCoin()));
        viewHolder.tv1.setText(bean.getStreamable_type_chinese());

        viewHolder.itemView.setTag(bean);
        viewHolder.itemView.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_count)
        TextView tvCount;
        @BindView(R.id.tv_amount)
        TextView tvAmount;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_1)
        TextView tv1;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CoinsBean bean = (CoinsBean) v.getTag();
            if(callback != null){
                callback.onReceiveValue(bean);
            }
        }
    };

    public void setOnItemClickListener(ValueCallback<CoinsBean> callback){

        this.callback = callback;
    }

    private SimpleDateFormat getTimeFormat(){
        SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL.get();
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            SDF_THREAD_LOCAL.set(simpleDateFormat);
        }
        return simpleDateFormat;
    }
}
